package com.lin.lazybone.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.lin.lazybone.R;
import com.lin.lazybone.adapter.OrderListAdapter;
import com.lin.lazybone.entity.Order;
import com.lin.lazybone.entity._User;
import com.lin.lazybone.ui.OrderActivity;
import com.lin.lazybone.ui.OrderListActivity;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by mr.lin on 2017/10/18.
 */

public class OrderFragment extends Fragment implements OrderListAdapter.Callback
{
    private View rootView;
    private TabLayout order_tablayout;
    private List<Order> orderList;
    private ListView order_ListView;
    //下载下来的全部订单保存位置，操作在orderList
    private List<Order> allOrderList;
    OrderListAdapter adapter;
    int i = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        if (null != rootView)
        {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (null != parent)
            {
                parent.removeView(rootView);
            }
        } else
        {
            rootView = inflater.inflate(R.layout.fragment_order, null);
            initData();
            initView(rootView);// 控件初始化
        }
        return rootView;
    }

    //初始化数据
    private void initData()
    {
        allOrderList = new ArrayList<>();
        orderList = new ArrayList<>();
//        for (int i = 0; i < 10; i++)
//        {
//            Order order = new Order();
//            order.setOrder_del_type("申通" + i);
//            order.setOrder_type(true);
//            order.setOrder_price("80");
//            orderList.add(order);
//            allOrderList.add(order);
//        }

        //开启新线程来获取订单列表
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                _User user = BmobUser.getCurrentUser(_User.class);
                //从服务器上获取订单列表
                BmobQuery<Order> query = new BmobQuery<Order>();
                //返回50条数据，如果不加上这条语句，默认返回10条数据
                query.setLimit(50);
                query.addWhereEqualTo("order_aid", user.getUsername());
                //执行查询方法
                query.findObjects(new FindListener<Order>()
                {
                    @Override
                    public void done(List<Order> object, BmobException e)
                    {
                        if (e == null)
                        {
                            //Toast.makeText(getActivity(), "查询成功：共"+ object.size() + "条数据。" + i, Toast.LENGTH_SHORT).show();
                            allOrderList.addAll(object);
                            getActivity().runOnUiThread(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    selectOrder(0);
                                }
                            });
                        } else
                        {
                            getActivity().runOnUiThread(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    Toast.makeText(getActivity(), "获取失败" + i, Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                });
            }
        }).start();

    }

    //初始化界面
    private void initView(View view)
    {
        order_ListView = (ListView) view.findViewById(R.id.order_List);
        order_tablayout = (TabLayout) view.findViewById(R.id.order_tablayout);
        order_tablayout.addTab(order_tablayout.newTab().setText("全部"));
        order_tablayout.addTab(order_tablayout.newTab().setText("待抢单"));
        order_tablayout.addTab(order_tablayout.newTab().setText("待送达"));
        order_tablayout.addTab(order_tablayout.newTab().setText("已送达"));

        adapter = new OrderListAdapter(getContext(), orderList, this, 0);
        order_ListView.setAdapter(adapter);

        //为TabLayout设置按钮监听事件
        order_tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
        {
            @Override
            public void onTabSelected(TabLayout.Tab tab)
            {
                //Toast.makeText(getActivity(), "dianjile" + tab.getPosition() + tab.getText(), Toast.LENGTH_SHORT).show();
                selectOrder(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab)
            {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab)
            {

            }
        });

    }

    //从全部的订单中选出相应状态
    private void selectOrder(int position)
    {
        orderList.clear();
        if (position != 0)
        {
            for (Order order : allOrderList)
            {
                if (position == order.getOrder_state())
                {
                    orderList.add(order);
                }
            }
        } else
        {
            orderList.addAll(allOrderList);
        }
        adapter.notifyDataSetChanged();
        order_ListView.setSelection(0);
        Toast.makeText(getActivity(), orderList.size() + "", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void buttonclick(View v)
    {
        int posotion = (Integer) v.getTag();
        switch (v.getId())
        {
            case R.id.orderlist_button_cancel:
                Toast.makeText(getActivity(), "点击了取消按钮" + posotion, Toast.LENGTH_SHORT).show();
                break;
            case R.id.orderlist_item_layout:
                //Toast.makeText(getActivity(), "点击了查看详情" + posotion, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), OrderActivity.class);
                intent.putExtra("order_data", orderList.get(posotion));
                intent.putExtra("type", 1);
                startActivity(intent);
                break;

        }
    }
}
